package com.wn.airplane_mode.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.wn.airplane_mode.R;
import com.wn.airplane_mode.activity.AirplaneModeOpNewActivity;
import com.wn.airplane_mode.receiver.AirplaneModeOffReceiver;
import com.wn.airplane_mode.receiver.AirplaneModeOnReceiver;
import com.wn.airplane_mode.tool.AirplaneTool;

/**
 * @author Grant Woo
 * 飞行模式服务管理*/
public class AirplaneModeOpService extends Service{

	private AirplaneTool tool = null;
	
	//private PendingIntent pendingIntent_on = null,pendingIntent_off = null;
	private AlarmManager alarm = null;
	
	private List<PendingIntent> taskList = new ArrayList<PendingIntent>(2);

	private NotificationManager	notificationManager = null;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		if(tool == null){
			tool = AirplaneTool.getInstance(this);
		}
		Log.i("AirplaneModeOpService", "onCreate...");
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		Log.i("AirplaneModeOpService", "onStart....");
		

		// pendingIntent_on = PendingIntent.getBroadcast(this, 0, new
		// Intent(this,AirplaneModeOnReceiver.class), 0);
		// pendingIntent_off = PendingIntent.getBroadcast(this, 0, new
		// Intent(this,AirplaneModeOffReceiver.class), 0);
		//
		// alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
		//
		// String[]time_on = tool.getOpenTime().split(Tool.SPLIT);
		// Calendar cal_on = Calendar.getInstance();
		// cal_on.set(Calendar.HOUR_OF_DAY,
		// Integer.valueOf(time_on[0]).intValue());
		// cal_on.set(Calendar.MINUTE, Integer.valueOf(time_on[1]).intValue());
		// //注册可重复的“闹钟”意图
		// alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal_on.getTimeInMillis(),
		// AlarmManager.INTERVAL_DAY, pendingIntent_on);
		//
		// String[]time_off = tool.getCloseTime().split(Tool.SPLIT);
		// Calendar cal_off = Calendar.getInstance();
		// cal_off.set(Calendar.HOUR_OF_DAY,
		// Integer.valueOf(time_off[0]).intValue());
		// cal_off.set(Calendar.MINUTE,
		// Integer.valueOf(time_off[1]).intValue());
		// //注册可重复的“闹钟”意图
		// alarm.setRepeating(AlarmManager.RTC_WAKEUP,
		// cal_off.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
		// pendingIntent_off);
		//
		// tool.setServiceRunning(true);

		alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
//		for(int i =1;i<=7;i++){
//			TaskObj obj = new TaskObj(tool.getTaskObjStr(i));
//			Log.i("AirplaneModeOpService", "onStart....obj="+obj.toString());
//			
//			Intent task_off = new Intent();
//			task_off.setAction("com.wn.airplane_mode.receiver.AirplaneModeOff");
//			task_off.putExtra(AirplaneTool.DAY_OF_WEEK_KEY, obj.getDayId());
//			task_off.putExtra(AirplaneTool.TASKOBJ_KEY, obj.toString());
//			setTask(task_off, obj.getTurnOffTime());
//			
//			Intent task_on = new Intent();
//			task_on.setAction("com.wn.airplane_mode.receiver.AirplaneModeOn");
//			task_on.putExtra(AirplaneTool.DAY_OF_WEEK_KEY, obj.getDayId());
//			task_on.putExtra(AirplaneTool.TASKOBJ_KEY, obj.toString());
//			setTask(task_on, obj.getTurnOnTime());
//			
//		}
		
		setTask(new Intent(this, AirplaneModeOnReceiver.class));
		setTask(new Intent(this, AirplaneModeOffReceiver.class));
		
//		String []times = tool.getTimeArray();
//		alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
//		for(String time:times){
//			if(time!=null){
//				String[] element = time.split(Tool.SPLIT_ELEMENT);
//				//激活状态
//				if(Boolean.parseBoolean(element[3])){
//					Intent task = null;
//					if(Byte.parseByte(element[1])== Tool.MODE_OPEN){
//						//开启飞行模式任务
//						task = new Intent(this,AirplaneModeOnReceiver.class);
//					}else{
//						task = new Intent(this,AirplaneModeOffReceiver.class);
//					}
//					
//					PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, task, 0);
//					String[] tasktime = element[2].split(Tool.SPLIT_H_M);
//					Calendar cal = Calendar.getInstance();
//					cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(tasktime[0]).intValue());
//					cal.set(Calendar.MINUTE, Integer.valueOf(tasktime[1]).intValue());
//					//注册可重复的“闹钟”意图
//					alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
//			
//					taskList.add(pendingIntent);
//				}
//				
//			}
//		}
		//tool.setServiceRunning(true);
		
		/*任务栏通知管理*/
		Intent in = new Intent(this,AirplaneModeOpNewActivity.class);//点击该通知后要转跳到的目标
		in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//如果已有Activity运行，则先清除运行的，再运行新的
		
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, in, 0);
		notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification();
		String contentTitle = getString(R.string.app_name);
		notification.tickerText = contentTitle;
		notification.icon = R.drawable.icon;
		notification.flags |= Notification.FLAG_ONGOING_EVENT;//将此通知放到通知栏的"Ongoing"即"正在运行"组中
		notification.flags |= Notification.FLAG_NO_CLEAR;//表明在点击了通知栏中的"清除通知"后，此通知不清除
		
		notification.setLatestEventInfo(this, contentTitle, getString(R.string.service_running), contentIntent);
		//发出通知
		notificationManager.notify(R.string.app_name, notification);
	}
	
//	private void setTask(Intent task,String timeStr){
//	
//		PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, task, 0);
//		
//		String[] tasktime = timeStr.split(AirplaneTool.SPLIT_H_M);
//		Calendar cal = Calendar.getInstance();
//		cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(tasktime[0]).intValue());
//		cal.set(Calendar.MINUTE, Integer.valueOf(tasktime[1]).intValue());
//		//注册可重复的“闹钟”意图
//		alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pendingIntent);
//
//		taskList.add(pendingIntent);
//	}
	
	private void setTask(Intent task){
		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, task, 0);
		//注册可重复的“闹钟”意图
		alarm.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 30*1000, pendingIntent);

		taskList.add(pendingIntent);
	}
	
	
	@Override
	public void onDestroy() {
		Log.i("AirplaneModeOpService", "onDestroy....begin,taskList size="+taskList.size());
		//取消服务
		// if(alarm!=null){
		// alarm.cancel(pendingIntent_on);
		// alarm.cancel(pendingIntent_off);
		// alarm = null;
		// pendingIntent_on = null;
		// pendingIntent_off = null;
		// }
		if(alarm!=null){
			for(PendingIntent pdi:taskList){
				alarm.cancel(pdi);
			}
			alarm = null;
			taskList.clear();
		}
		

		//tool.setServiceRunning(false);
		if(tool != null){
			tool = null;
		}
		
		
		//清除任务栏通知提示
		if(notificationManager != null){
			notificationManager.cancel(R.string.app_name);
		}
		
		Log.i("AirplaneModeOpService", "onDestroy....end");
	}
}
